// pages/useEquipment/useEquipment.js
import {
  request
} from '../../request/index.js'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    index: 1,
    click: false,
    hot: false,
    mode: false,
    dry: false,
    start: false,
    id1: 0,
    duration: 0,
    wash_machine_picture: '', //上一个页面的洗衣机图片
    clothList: [],
    time: '15',
    tem: '65',
    index1: 1,
    functionList: [],
    user: {},
    machineStatus: 2,
    temprature: {}, //快洗的对象
    machineid: 0,
    washtime: 0,
    money1: 0, //加热
    money2: 0, //杀菌
    money3: 0, //干衣
    money4: 0, //洗衣模式
    onemoney:0,
    twomoney:0,
    threemoney:0,
    hotmoney:0,
    modemoney:0,
    drymoney:0,
    total:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 从上个页面传入的洗衣机id
    let machineid = parseInt(options.machineid);
    this.setData({
      machineid,
    });
    request({
      url: 'WashMachine/getFunction',
      data: {
        wash_machine_id: machineid
      },
    }).then((result) => {
      console.log(result);
      let functionList = result.data.data.functionList;
      this.setData({
        functionList,
        machineStatus: result.data.data.wash_machine_state,
        washtime: functionList[0].function_default, //洗衣时间
        money4: functionList[0].function_money, //快洗金额
        temprature: functionList[3], //快洗功能对象
        duration: functionList[3].function_min, //在页面展示
        hotmoney:functionList[3].function_money,
        modemoney:functionList[4].function_money,
        drymoney:functionList[5].function_money,
        onemoney:functionList[0].function_money,
        twomoney:functionList[1].function_money,
        threemoney:functionList[2].function_money,
      })
    });
  },
  onShow: function () {
    let user = wx.getStorageSync('user');
    // let user = typeof wx.getStorageSync('user') === 'string' ? JSON.parse(wx.getStorageSync('user')) : wx.getStorageSync('user');
    //上一个页面传入的功能列表
    // let functionList = wx.getStorageSync('functionList')
    // let functionList = typeof wx.getStorageSync('functionList') === 'string' ? JSON.parse(wx.getStorageSync('functionList')) : wx.getStorageSync('functionList');
    this.setData({
      user,
    });
  },
  //点击加热时触发
  hotClick(e) {
    let hot = this.data.hot;
    this.setData({
      hot: !hot,
      id1: e.currentTarget.dataset.id1
    });
    if (this.data.hot) {
      this.setData({
        money1: 0.2,
      });
    } else {
      this.setData({
        money1: 0,
      })
    }
  },
  //点击杀菌时触发
  modeClick(e) {
    let mode = this.data.mode;
    this.setData({
      mode: !mode,
      id1: e.currentTarget.dataset.id1
    });
    if (this.data.mode) {
      this.setData({
        money2: this.data.functionList[4].function_money,
      })
    } else {
      this.setData({
        money2: 0,
      })
    }
  },
  //点击干衣时触发
  dryClick(e) {
    let dry = this.data.dry;
    this.setData({
      dry: !dry,
      id1: e.currentTarget.dataset.id1,
    });
    if (this.data.dry) {
      this.setData({
        money3: this.data.functionList[5].function_money,
      });
      console.log(this.data.money3);
    } else {
      this.setData({
        money3: 0,
      })
    }
    request({
      url: 'typeofclothing/getAllTypeofclothing',
      data: {},
    }).then((result) => {
      console.log(result);
      this.setData({
        clothList: result.data
      })
    });
  },
  //点击洗衣模式触发
  changeClick2(e) {
    let index = parseInt(e.currentTarget.dataset.index);
    this.setData({
      index,
    });
    if (e.currentTarget.dataset.index == 1) {
      this.setData({
        money4: this.data.functionList[0].function_money,
        washtime: this.data.functionList[0].function_default
      })
    } else if (e.currentTarget.dataset.index == 2) {
      this.setData({
        money4: this.data.functionList[1].function_money,
        washtime: this.data.functionList[1].function_default
      })
    } else {
      this.setData({
        money4: this.data.functionList[2].function_money,
        washtime: this.data.functionList[2].function_default
      })
    }
  },

  //温度被改变时触发
  durationChange(e) {
    this.setData({
      duration: e.detail.value,
      money1: parseFloat((this.data.functionList[3].function_money) * (this.data.duration)).toFixed(2),
    });
  },
  //点击干衣下的衣物类别时触发
  clothClick(e) {
    console.log(e.currentTarget.dataset.id);
    let index1 = e.currentTarget.dataset.id;
    this.setData({
      index1
    })
    this.setData({
      time: e.currentTarget.dataset.time,
      tem: e.currentTarget.dataset.tem
    })
  },




  /**
   * 点击确定显示支付密码输入层
   */
  showInputLayer() {
    let user = this.data.user;
    if (user == '' || Object.keys(user).length == 0) {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      })
    } else {
      if (this.data.machineStatus == 1) {
        this.hidePayLayer('设备正在使用中', 'none');
      } else if (this.data.machineStatus == 3) {
        this.hidePayLayer('设备已停用', 'none');
      } else if (this.data.machineStatus == 0) {
        if (user.safetyInfo.pay_password == null || this.data.user.safetyInfo.pay_password == '') {
          wx.navigateTo({
            url: '../../pages/setPass/setPass'
          })
        } else {
          let total =(parseFloat(this.data.money1) +parseFloat(this.data.money2)+parseFloat(this.data.money3)+parseFloat(this.data.money4)).toFixed(2) ;
          this.setData({
            showPayPwdInput: true,
            payFocus: true,
            total
          });
        }
      }
    }

  },

  // 点击返回、忘记密码触发的函数
  hidePayLayer1() {
    this.hidePayLayer('取消支付', 'none')
  },
  /**
   * 隐藏支付密码输入层
   */
  hidePayLayer(text, icon) {

    this.setData({
        showPayPwdInput: false,
        payFocus: false,
        pwdVal: ''
      },
      function () {
        wx.showToast({
          title: text,
          icon: icon
        })
      });

  },
  /**
   * 获取焦点
   */
  getFocus() {
    this.setData({
      payFocus: true
    });
  },
  /**
   * 输入密码监听
   */
  inputPwd(e) {
    this.setData({
      pwdVal: e.detail.value
    });
    if (e.detail.value.length >= 6 && this.data.user.safetyInfo.pay_password == e.detail.value && this.data.total <= this.data.user.safetyInfo.account_money) {
      this.hidePayLayer('支付成功', 'success');
      // 启动洗衣机发送请求
      let start = this.data.start;
      this.setData({
        start: !start
      })
      if (!start) {
        let text = (this.data.hot ? '加热 ' : ' ') + (this.data.mode ? '杀菌 ' : ' ') +
          (this.data.dry ? '干衣 ' : ' ') + (this.data.index == 1 ? '快洗 ' : ' ') +
          (this.data.index == 2 ? '标准洗 ' : ' ') + (this.data.index == 3 ? '大物件 ' : ' ');
        request({
          url: 'Order/addOrder',
          data: {
            user_id: this.data.user.user_id,
            order_remark: text,
            wash_time: this.data.washtime,
            wash_machine_id: this.data.machineid,
            consume_money: this.data.total
          },
        }).then((result) => {
          console.log(result);
          wx.setStorageSync('user', result.data.data);
          this.setData({
            user: result.data.data
          });
          wx.switchTab({
            url: '../../pages/active/active'
          })
        });
      }
    } else if (e.detail.value.length >= 6 && this.data.user.safetyInfo.pay_password != e.detail.value) {
      this.hidePayLayer('密码输入不正确', 'none');
    } else if (e.detail.value.length >= 6 && this.data.total > this.data.user.safetyInfo.account_money) {
      this.hidePayLayer('您的余额不够，请充值', 'none');
      wx.navigateTo({
        url: '../../pages/recharge/recharge',
      })
    }
  },
  goPass(){
    wx.navigateTo({
      url: '../editPass/editPass',
    })
  }

})